package common

import (
	"context"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/common"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/common/file"
	"git.myscrm.cn/golang/stark/v4"
)

type fileServer struct {
	fileService file.FileServiceIface
}

func NewFileServer(fileService file.FileServiceIface) pb.FileServiceServer {
	return &fileServer{
		fileService: fileService,
	}
}

func (s *fileServer) SaveToFile(ctx context.Context, request *pb.SaveToFileRequest) (*pb.SaveToFileResponse, error) {
	fileId, err := s.fileService.SaveFile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc SaveToFile request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	resp := &pb.SaveToFileResponse{
		FileId: fileId,
	}

	return resp, nil
}

func (s *fileServer) GetFileInfo(ctx context.Context, request *pb.GetFileInfoRequest) (*pb.GetFileInfoResponse, error) {
	fileInfo, err := s.fileService.GetFileInfo(ctx, request.FileId)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetFileInfo request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	resp := &pb.GetFileInfoResponse{
		FileInfo: &pb.FileInfo{
			FileId:       fileInfo.Id,
			Business:     fileInfo.Business,
			FileName:     fileInfo.FileName,
			FileType:     fileInfo.FileType,
			FilePath:     fileInfo.FilePath,
			FileSize:     fileInfo.FileSize,
			CreatedTime:  fileInfo.CreatedOn.Format("2006-01-02 15:04:05"),
			ModifiedTime: fileInfo.ModifiedOn.Format("2006-01-02 15:04:05"),
		},
	}
	return resp, nil
}
